package com.easylinkin.linkappapi.shigongyun.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.bases.redis.util.RedisUtil;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import com.easylinkin.linkappapi.shigongyun.service.SiteMonitorService;
import com.easylinkin.linkappapi.shigongyun.vo.MonitorInfoVo;
import com.easylinkin.linkappapi.shigongyun.vo.MonitorPointTypeVo;
import com.easylinkin.linkappapi.shigongyun.vo.MonitorRecordVo;
import com.easylinkin.linkappapi.webcammanage.entity.DeviceAttributeStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.util.Date;
import java.util.List;

/**
 * 基坑监测大屏接口
 *
 * @author zhengwen
 **/
@Slf4j
@RestController
@Api(value = "基坑监测大屏", tags = {"基坑监测大屏接口"})
@RequestMapping("/siteMonitor")
public class SiteMonitorController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SiteMonitorService siteMonitorService;

    @Autowired
    private LinkappUserContextProducer linkappUserContextProducer;

    /**
     * redisKey基坑监测模块缓存前缀
     */
    private final static String redisKeyBase = "siteMonitor:";


    /**
     * 监测点位分类数据
     *
     * @param monitorPointTypeVo 监测点位分类入参vo
     * @return 统一出参
     */
    @PostMapping("/monitorPointTypeData")
    @ApiOperation("获取监测点位分类数据")
    public RestMessage monitorPointTypeData(@RequestBody MonitorPointTypeVo monitorPointTypeVo) {
        String monitorPointTypeResDataKey = redisKeyBase + "monitorPointType:resData";
        LinkappUser linkappUser = linkappUserContextProducer.getCurrent();
        if (linkappUser != null) {
            monitorPointTypeResDataKey += ":" + linkappUser.getTenantId();
        }

        String resData = null;
        Object resDataObj = redisUtil.get(monitorPointTypeResDataKey);
        if (resDataObj != null) {
            resData = resDataObj.toString();
        }
        if (StringUtils.isNotBlank(resData)) {
            JSONArray jsonArray = JSONUtil.parseArray(resData);
            return RestBuilders.successBuilder().data(jsonArray).build();
        }
        return RestBuilders.successBuilder().data(null).build();
    }

    /**
     * 监测数据趋势
     *
     * @param monitorInfoVo 监测数据趋势入参vo
     * @return 统一出参
     */
    @PostMapping("/latestMonitorDataTrend")
    @ApiOperation("获取监测数据趋势")
    public RestMessage latestMonitorDataTrend(@RequestBody MonitorInfoVo monitorInfoVo) {
        Date startTime = monitorInfoVo.getStartTime();
        if (startTime == null) {
            startTime = DateUtil.offsetDay(DateUtil.date(), -7);
            monitorInfoVo.setStartTime(startTime);
        }
        return siteMonitorService.latestMonitorDataTrend(monitorInfoVo);
    }

    /**
     * 获取实时监测数据
     *
     * @param monitorInfoVo 实时监测数据入参vo
     * @return 统一出参
     */
    @PostMapping("/realTimeMonitorData")
    @ApiOperation("获取实时监测数据")
    public RestMessage realTimeMonitorData(@RequestBody MonitorInfoVo monitorInfoVo) {
        Date startTime = monitorInfoVo.getStartTime();
        if (startTime == null) {
            startTime = DateUtil.offsetDay(DateUtil.date(), -7);
            monitorInfoVo.setStartTime(startTime);
        }
        return siteMonitorService.realTimeMonitorData(monitorInfoVo);
    }

    /**
     * 获取摄像头设备列表
     *
     * @param requestModel 请求入参对象
     * @return 统一出参
     */
    @PostMapping("/getVideoList")
    public RestMessage getVideoList(@RequestBody RequestModel<Device> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");

        IPage<DeviceAttributeStatus> deviceVideoList = siteMonitorService.getVideoList(
                requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(deviceVideoList).build();
    }

    /**
     * 根据设备id获取摄像头信息
     *
     * @param id 设备id
     * @return 统一出参
     */
    @GetMapping("/getVideoInfo/{id}")
    public RestMessage getVideoInfo(@PathVariable String id) {
        Assert.isTrue(StringUtils.isNotBlank(id), "ID不能为空");

        List<DeviceAttributeStatus> videoAttrInfo = siteMonitorService.getVideoInfo(id);
        return RestBuilders.successBuilder().data(videoAttrInfo).build();
    }

    /**
     * 获取最新监测项目名称数据
     *
     * @param monitorInfoVo 监测对象vo
     * @return 统一出参
     */
    @PostMapping("/getLatestMonitorNameList")
    @ApiOperation("获取最新监测项目名称数据")
    public RestMessage getLatestMonitorNameList(@RequestBody MonitorInfoVo monitorInfoVo) {
        //String projectId = monitorInfoVo.getProjectId();
        //Assert.isTrue(StringUtils.isNotBlank(projectId), "基坑所属项目ID为空");
        if (monitorInfoVo == null) {
            monitorInfoVo = new MonitorInfoVo();
        }
        Date startTime = monitorInfoVo.getStartTime();
        if (startTime == null) {
            startTime = DateUtil.offsetDay(DateUtil.date(), -7);
            monitorInfoVo.setStartTime(startTime);
        }
        return siteMonitorService.getLatestMonitorNameList(monitorInfoVo);
    }

    /**
     * 获取最新监测项测点下拉数据
     *
     * @param monitorInfoVo 监测对象vo
     * @return 统一出参
     */
    @PostMapping("/getLatestMonitorPointNameList")
    @ApiOperation("获取最新监测项测点下拉数据")
    public RestMessage getLatestMonitorPointNameList(@RequestBody MonitorInfoVo monitorInfoVo) {
        //String projectId = monitorInfoVo.getProjectId();
        //Assert.isTrue(StringUtils.isNotBlank(projectId), "基坑所属项目ID为空");
        if (monitorInfoVo == null) {
            monitorInfoVo = new MonitorInfoVo();
        }

        Date startTime = monitorInfoVo.getStartTime();
        if (startTime == null) {
            startTime = DateUtil.offsetDay(DateUtil.date(), -7);
            monitorInfoVo.setStartTime(startTime);
        }
        return siteMonitorService.getLatestMonitorPointNameList(monitorInfoVo);
    }

    /**
     * 获取监测项目K线数据
     *
     * @param monitorRecordVo 监测项目入参vo
     * @return 统一出参
     */
    @PostMapping("/getMonitorKxData")
    @ApiOperation("获取监测项目K线数据")
    public RestMessage getMonitorKxData(@RequestBody MonitorRecordVo monitorRecordVo) {

        return siteMonitorService.getMonitorKxData(monitorRecordVo);
    }

    /**
     * 获取测点详情
     *
     * @param monitorRecordVo 监测项目入参vo
     * @return 统一出参
     */
    @PostMapping("/getPointDetail")
    @ApiOperation("获取测点详情")
    public RestMessage getPointDetail(@RequestBody MonitorRecordVo monitorRecordVo) {

        return siteMonitorService.getPointDetail(monitorRecordVo);
    }

    /**
     * 获取配置的视频信息
     *
     * @return 统一出参
     */
    @GetMapping("/getConfigVideoInfo")
    public RestMessage getConfigVideoInfo() {
        List<DeviceAttributeStatus> videoAttrInfo = siteMonitorService.getConfigVideoInfo();
        return RestBuilders.successBuilder().data(videoAttrInfo).build();
    }

}
